import {createRouter, createWebHashHistory} from 'vue-router'
import store from "@/store/index"
import {DeviceRouter} from "./device_router"
// 必须要有一个初始化
import Login from "../views/login/login_layout"
import Index from "../views/menu/ant_menu";
// import Index from "../views/menu/index";
import {SystemUser} from "@/router/system_user";
import {CockpitRouter} from "@/router/cockpit_router";
import {StatisticsRouter} from "@/router/statistics_router";
import {TestRouter} from "@/router/test_router";
import {message} from "ant-design-vue";
// import {AllRouter} from "@/router/all_router";


const menu_routes = [
    ...SystemUser, ...DeviceRouter, ...CockpitRouter, ...StatisticsRouter, ...TestRouter
]


const routes = [
    {
        path: "",
        title: "入口",
        redirect: "/login",
        children: []
    },
    {
        path: '/index',
        title: "菜单",
        name: "菜单",
        meta: {
            keepAlive: false
        },
        component: Index,
        children: menu_routes
    },
    {
        path: '/login',
        title: "登录",
        name: '登录',
        meta: {
            keepAlive: false
        },
        component: Login
    },
    {
        path: '/index_exception',
        title: "异常界面",
        name: 'index_exception',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/exception_page/index_exception.vue'),
        meta: {
            keepAlive: false
        },
    },
    {
        path: '/device_control_join',
        title: "设备查询入口",
        name: 'index_exception',
        // route level code-splitting
        // this generates a separate chunk (about.[hash].js) for this route
        // which is lazy-loaded when the route is visited.
        component: () => import(/* webpackChunkName: "about" */ '../views/cockpit/device_control_join.vue'),
        meta: {
            keepAlive: false
        },
    },
    {}
]

const router = createRouter({
    // history: createWebHistory(process.env.BASE_URL),
    history: createWebHashHistory(),
    routes
})
const can_pass = [
    "/login",
    //生成的溯源记录表
    "/index/export_echarts"
]
router.beforeEach((to, from, next) => {
    // NProgress.start();
    let status = false
    // 去登录页，下面不需要执行
    if (to.path === "/login") {
        // NProgress.done();
        return next();
    }
    if (!store.state.user.token) {
        // NProgress.done();

        //没有登录，去跳转登录页
        // console.log(can_pass.includes(to.path))
        try {
            const index = can_pass.indexOf(to.path)
            if (index > -1) {
                status = true;
            }
        } catch (e) {
            status = false
        }
        if (status) {
            if (to.path.includes("/index/statistics/export_echarts")) {
                try {
                    if (to.query.id && to.query.filename && to.query.inputTime) {
                        return next()
                    } else {
                        return next({path: "/login"})
                    }
                } catch (e) {
                    console.log("参数异常")
                    message.warning("参数异常")
                    return next({patch: "/login"})
                }
            }
        } else if (to.path === "/login") {
            return next()
        } else if (
            to.path === "/index/cockpit/device_data_details" ||
            (to.path === "/device_control_join") ||
            (to.path === "/index/cockpit/data_analysis") ||
            (to.path === "/index/test/demo_device")
        ) {
            return next()
        } else {
            return next({path: "/login"});
        }
    } else {
        next();
    }
});

router.afterEach(() => {

});

//重置state
store.commit('resetState')
window.onbeforeunload = function (e) {
    store.commit('saveState')
}

export default router
